/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ 
package com.airlogger.log.view.format 
{
	import com.bourre.collection.HashMap;	
	import com.airlogger.log.model.LogData;	
	import com.airlogger.log.view.format.Formatter;
	
import flash.utils.getQualifiedClassName;	/**
	 * @author Cedric Nehemie
	 */
	public class EntryFormatter implements Formatter 
	{
		static protected const DEFAULT : String = "FORMATTER_DEFAULT";
		static protected const DETAILS : String = "FORMATTER_DETAILS";
	
		protected var _oMap : HashMap;
			
		public function EntryFormatter()
		{
			_oMap = createDefaultMap();
		}

		public function format (data : LogData) : String
		{
			var level : String = data.level.getName();
			var type : String = getQualifiedClassName( data.message );
			var message : String;
			var details : String;
			
			details = ( _oMap.get( DETAILS ) as Formatter ).format( data );
			if( _oMap.containsKey( type ) )
			{
				message = ( _oMap.get( type ) as Formatter ).format( data );
			}
			else
			{
				message = ( _oMap.get( DEFAULT ) as Formatter ).format( data );
			}
			
			var logContent : String = "<div class='log-message'>" + message + "</div>";
			var logDetails : String = "<div class='log-info closed'>" + details + "</div>";
			
			var log : String = "<div class='" + level + "'>" +
									"<table class='log' cellpadding='0' cellspacing='0' border='0' width='100%'>" +
									"<tr><td class='log-info-control' onclick='toggleDetails(this);'>" +
										"<img src='../img/plus.gif'/>" +
										"</td>" +
									"<td class='log-content'>" +
											logContent + 
											logDetails +
									"</td></tr></table>" +
								"</div>";
			return log;
		}
		
		private function createDefaultMap () : HashMap
		{
			var map : HashMap = new HashMap();
			
			map.put( DEFAULT, new DefaultFormatter() );
			map.put( DETAILS, new DetailsFormatter() );
			
			map.put( "XML", new XMLFormatter() );
			map.put( "Object", new ObjectFormatter() );	
			map.put( "Array", new ArrayFormatter() );
			
			return map;
		}
	}
}
